<template>
  <div class="home-contianer">
    <el-container>
      <el-header>
        <img class="logo" src="@/assets/logo-w.png" width="120" alt="">
        <strong>电商管理系统</strong>
        <div class="user-box">
          <!-- <img src="@/assets/hd.png" height="40" alt=""> -->
          <el-avatar icon="el-icon-user-solid"></el-avatar>
          <span>{{userName}}</span> | <i class="el-icon-switch-button exit" @click="exit"></i>
        </div>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <el-menu default-active="/home/welcome" class="el-menu-vertical-demo" active-text-color='#6a9d03' router>
            <el-menu-item index="/home/welcome">
              <span slot="title">系统主页</span>
            </el-menu-item>
            <!-- 渲染菜单 -->
            <el-submenu :index="item.id+''" v-for="item in menuList" :key="item.id">
              <template slot="title">
                <span>{{item.authName}}</span>
              </template>
              <el-menu-item :index="'/home'+item2.path" v-for="item2 in item.children" :key="item2.id"><i class="iconfont icon-dian"></i>{{item2.authName}}</el-menu-item>
            </el-submenu>
            <el-menu-item index="/home/stat">
              <span slot="title">数据统计</span>
            </el-menu-item>
          </el-menu>
        </el-aside>
        <el-main>
          <!-- home组件的子路由 -->
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>
<script>
export default {
  name: 'HomeCom',
  data() {
    return {
      userName: '',
      menuList: []
    }
  },
  methods: {
    exit() {
      this.$router.replace('/login')
      // localStorage.removeItem('token')
      localStorage.clear()
      this.$message({
        message: '已退出',
        type: 'success'
      })
    },
    // 显示对应的菜单列表
    async getMenu(rid) {
      const { data: rel } = await this.$http.get(`menu?roleId=${rid}`)
      if (rel.code === 20000) {
        this.menuList = rel.data
      }
    }
  },
  created() {
    const rid = localStorage.getItem('userrole')
    this.getMenu(rid)
    // 显示当前登录用户名
    const username = localStorage.getItem('username')
    if (username) {
      this.userName = username
    }
  }
}
</script>

<style scoped>
.home-contianer {
  height: 100%;
}
.el-container {
  height: 100%;
}
.el-header {
  background-color: #6a9d03;
  position: relative;
  padding-left: 150px;
  line-height: 60px;

  color: #fff;
}
.el-header strong {
  font-size: 24px;
}
.el-header .logo {
  position: absolute;
  left: 20px;
  top: 50%;
  transform: translateY(-50%);
}
.el-aside {
  height: 100%;
}
.el-main {
  height: 100%;
  border-left: 1px solid #f1f1f1;
}
.user-box {
  height: 40px;
  position: absolute;
  right: 0;
  top: 50%;
  transform: translateY(-50%);
  line-height: 40px;
  display: flex;
}
.user-box img {
  vertical-align: -webkit-baseline-middle;
}
.user-box span {
  padding: 0 10px;
}
.user-box .el-icon-switch-button {
  font-size: 24px;
  padding: 0 20px;
  line-height: 40px;
}
.el-menu-item,
/deep/ .el-submenu__title {
  border-bottom: 2px solid #f1f1f1;
  text-align: center;
  font-weight: bold;
  font-size: 16px;
  color: #666;
}
.el-submenu .el-menu-item {
  font-size: 14px;
  font-weight: normal;
}
/deep/ .el-submenu.is-active .el-submenu__title {
  border-bottom-color: #f1f1f1 !important;
}
.exit:hover {
  color: yellow;
  cursor: pointer;
}
</style>
